package com.shou.zhnfyxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shou.zhnfyxt.Dto.UserDetailRoominfoDto;
import com.shou.zhnfyxt.entity.R;
import com.shou.zhnfyxt.entity.User;
import com.shou.zhnfyxt.entity.UserDetailRoominfo;
import com.shou.zhnfyxt.mapper.UserDetailRoominfoMapper;
import com.shou.zhnfyxt.mapper.UserMapper;
import com.shou.zhnfyxt.service.UserDetailRoominfoService;
import com.shou.zhnfyxt.service.UserService;
import com.shou.zhnfyxt.utils.JwtUtil;
import com.shou.zhnfyxt.utils.RedisCache;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
@RequestMapping("/userdetail")
public class UserDetailRoominfoController {

    @Autowired
    private UserDetailRoominfoService userDetailRoominfoService;
    @Autowired
    private UserDetailRoominfoMapper userDetailRoominfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisCache redisCache;

    @PostMapping("userdetailStepOne")
    public R<UserDetailRoominfo> SaveUserDetailRoominfo (@RequestBody UserDetailRoominfoDto userDetailRoominfoDto){
        //获取前端数据
        String city = userDetailRoominfoDto.getCity();
        String targetcity = userDetailRoominfoDto.getTargetcity();
        String housingtype = userDetailRoominfoDto.getHousingtype();
        List<String> options = userDetailRoominfoDto.getOptions();
        String price = userDetailRoominfoDto.getPrice();
        String squire= userDetailRoominfoDto.getSquire();
        String username = userDetailRoominfoDto.getUsername();

        //处理数据保存至Dao中
        UserDetailRoominfo userDetailRoominfo = new UserDetailRoominfo();
        userDetailRoominfo.setUserId(username);
        userDetailRoominfo.setCityNow(city);
        userDetailRoominfo.setCityTarget(targetcity);
        userDetailRoominfo.setRoomPrice(price);
        userDetailRoominfo.setRoomType(housingtype);
        userDetailRoominfo.setRoomSquire(squire);
        String roomOption = options.get(0) + options.get(1) + options.get(2);
        userDetailRoominfo.setRoomOption(roomOption);

        //保存到数据库
        userDetailRoominfoService.save(userDetailRoominfo);

        User user = userMapper.selectById(username);
        user.setStatus(String.valueOf(1));
        userService.updateById(user);
        return R.success(userDetailRoominfo);
    }

    @PostMapping("getroominfo")
    public R<UserDetailRoominfo> getUserDetailRoominfo(@RequestHeader String token){
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        System.out.println(userId);
        QueryWrapper<UserDetailRoominfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" , userId);
        UserDetailRoominfo userDetailRoominfo = userDetailRoominfoMapper.selectOne(wrapper);
        return R.success(userDetailRoominfo);
    }

    @GetMapping("getUserTargetCity")
    public R<String> getUserTargetCity(@RequestHeader String token){
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        QueryWrapper<UserDetailRoominfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , userId);
        UserDetailRoominfo userDetailRoominfo = userDetailRoominfoMapper.selectOne(queryWrapper);
        String city = userDetailRoominfo.getCityTarget();
        return R.success(city);
    }

    @PutMapping("changeTargetCity")
    public R<String> changeTargetCity(@RequestBody String cityTarget, @RequestHeader String token){
        System.out.println(cityTarget);
//        String cityTarget = (String) map.get("cityTarget");
        System.out.println(cityTarget);
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        QueryWrapper<UserDetailRoominfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        UserDetailRoominfo userDetailRoominfo = userDetailRoominfoService.getOne(queryWrapper);
        userDetailRoominfo.setCityTarget(cityTarget);
        boolean update = userDetailRoominfoService.update(userDetailRoominfo, queryWrapper);
        if (update){
            return R.success(userDetailRoominfo.getCityTarget());
        }
        return R.error(999);
    }
    @GetMapping("getUserRoomInfo")
    public R<UserDetailRoominfo> getUserRoomInfo(@RequestHeader String token){
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        System.out.println(userId);
        QueryWrapper<UserDetailRoominfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" , userId);
        UserDetailRoominfo userDetailRoominfo = userDetailRoominfoService.getOne(wrapper);
        return R.success(userDetailRoominfo);
    }
    @PostMapping("updateRoomInfo")
    public R<String> updateUserInfo(@RequestBody UserDetailRoominfo userDetailRoominfo){
        QueryWrapper<UserDetailRoominfo> queryWrapper = new QueryWrapper<>();
        String userId = userDetailRoominfo.getUserId();
        queryWrapper.eq("user_id",userId);
        userDetailRoominfoService.update(userDetailRoominfo,queryWrapper);
        return R.success("success");
    }
}
